package com.wangxinenpu.springbootdemo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wangxinenpu.springbootdemo.dao.mapper.UserMapper;
import com.wangxinenpu.springbootdemo.dataobject.dto.user.UserListDto;
import com.wangxinenpu.springbootdemo.dataobject.model.Organization;
import com.wangxinenpu.springbootdemo.dataobject.model.Role;
import com.wangxinenpu.springbootdemo.dataobject.model.User;
import com.wangxinenpu.springbootdemo.dataobject.model.UserRole;
import com.wangxinenpu.springbootdemo.dataobject.vo.user.UserVo;
import com.wangxinenpu.springbootdemo.service.OrganizationService;
import com.wangxinenpu.springbootdemo.service.RoleService;
import com.wangxinenpu.springbootdemo.service.UserRoleService;
import com.wangxinenpu.springbootdemo.service.UserService;
import lombok.Setter;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author =
 * @since 2024-06-14
 */
@Service
@Setter ( onMethod_ = @Resource )
public class UserServiceImpl extends ServiceImpl < UserMapper,User > implements UserService {
	private RoleService roleService;
	private UserRoleService userRoleService;
	private OrganizationService organizationService;
	@Override
	public R < Page < UserVo > > pageList( UserListDto dto ) {

		// 所有组织
		Map < String,String > organizationMap = organizationService.list().stream().collect( Collectors.toMap( Organization::getId ,
				Organization::getOrgName ) );
		// 所有角色
		Map < String,String > roleMap = roleService.list().stream().collect( Collectors.toMap( Role::getId ,
				Role::getRoleName ) );

		Page < User > page = new Page <>( dto.getPageNum() , dto.getPageSize() );
		Page < User > userPage = page( page , new LambdaQueryWrapper < User >()
				.like( StringUtils.isNotBlank( dto.getUserNo()  ) , User::getUserNo , dto.getUserNo() )
				.like( StringUtils.isNotBlank( dto.getUserName()  ) , User::getUserName , dto.getUserName() )
				.eq( StringUtils.isNotBlank( dto.getOrgId()  ) , User::getOrgId , dto.getOrgId() )
		);

		List <UserVo> voList = new ArrayList <>();
		userPage.getRecords().forEach( user -> {
			UserVo userVo = new UserVo();
			BeanUtils.copyProperties( user , userVo );
			userVo.setOrgName( organizationMap.get( user.getOrgId() ) );
			List < String > roleIds = userRoleService.list( new LambdaQueryWrapper < UserRole >()
					.eq( UserRole::getUserId , user.getId() ) )
					.stream().map( UserRole::getRoleId ).collect( Collectors.toList() );
			List < String > roleNames = roleIds.stream().map( roleMap::get ).collect( Collectors.toList() );
			userVo.setRoleIds( roleIds );
			userVo.setRoleNames( roleNames );
			voList.add( userVo );
		} );


		Page < UserVo > voPage = new Page <>( dto.getPageNum() , dto.getPageSize() );
		voPage.setRecords( voList );
		voPage.setTotal( userPage.getTotal() );

		return R.ok( voPage );
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public R < Boolean > delete( List < String > ids ) {
		return R.ok( removeByIds( ids ) );
	}

	@Override
	public R < UserVo > detail( String id ) {

		// 所有组织
		Map < String,String > organizationMap = organizationService.list().stream().collect( Collectors.toMap( Organization::getId ,
				Organization::getOrgName ) );
		// 所有角色
		Map < String,String > roleMap = roleService.list().stream().collect( Collectors.toMap( Role::getId ,
				Role::getRoleName ) );

		User user = getById( id );
		UserVo userVo = new UserVo();
		BeanUtils.copyProperties( user , userVo );
		userVo.setOrgName( organizationMap.get( user.getOrgId() ) );
		List < String > roleIds = userRoleService.list( new LambdaQueryWrapper < UserRole >()
						.eq( UserRole::getUserId , user.getId() ) )
				.stream().map( UserRole::getRoleId ).collect( Collectors.toList() );
		List < String > roleNames = roleIds.stream().map( roleMap::get ).collect( Collectors.toList() );
		userVo.setRoleIds( roleIds );
		userVo.setRoleNames( roleNames );

		return R.ok( userVo );
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public R < Boolean > savaOrUpdate( UserVo dto ) {
		User user = new User();
		BeanUtils.copyProperties( dto , user);

		// 角色的保存
		userRoleService.remove( new LambdaQueryWrapper <UserRole>().eq( UserRole::getUserId , dto.getId() ) );

		dto.getRoleIds().forEach( roleId -> {
			UserRole userRole = new UserRole();
			userRole.setUserId( dto.getId() );
			userRole.setRoleId( roleId );
			userRoleService.save( userRole );
		} );

		return R.ok( saveOrUpdate( user ) );
	}

	@Override
	public R < List < Role > > getRoleList( String orgId ) {
		List < Role > roleList = roleService.list( new LambdaQueryWrapper < Role >().eq( Role::getOrgId , orgId ) );
		return R.ok( roleList );
	}
}
